package com.sign2024.sign2024project.capitalconstruction.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.capitalconstruction.JjHistoricalMajor;
import com.sign2024.sign2024core.entry.capitalconstruction.JjMajorCollect;
import com.sign2024.sign2024core.entry.projecttask.RollbackRecord;
import com.sign2024.sign2024project.capitalconstruction.mapper.JjHistoricalMajorMapper;
import com.sign2024.sign2024project.capitalconstruction.mapper.JjMajorCollectMapper;
import com.sign2024.sign2024project.capitalconstruction.param.JjMajorParam;
import com.sign2024.sign2024project.capitalconstruction.service.JjMajorCollectService;
import com.sign2024.sign2024project.projecttask.mapper.RollbackRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基建专业汇总表导入 业务层
 *
 * @author yujie
 * @data 2024/8/24 11:55
 */
@Service
public class JjMajorCollectServiceImpl implements JjMajorCollectService {

    @Resource
    private JjMajorCollectMapper jjMajorCollectMapper;

    @Resource
    private JjHistoricalMajorMapper jjHistoricalMajorMapper;

    @Resource
    private RollbackRecordMapper rollbackRecordMapper;

    @Override
    public void insertBatchJjMajorCollect(List<JjMajorCollect> list, Integer taskId) {
        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "任务id为空");
        } else {
            // 批量新增
            jjMajorCollectMapper.insertBatchJjMajor(list, taskId);
        }
    }

    @Override
    public void exportJjMajorCollect(List<JjMajorCollect> list, String fileName) {
        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        }

        // 获取 历史中 的 最版本号
// 获取历史版本号
        int code = 0;
        Integer versionCode = jjHistoricalMajorMapper.countMaxVersionCode();
        if (versionCode != null) {
            code = versionCode + 1;
        } else {
            code = code + 1;
        }

        List<JjMajorCollect> listAll = jjMajorCollectMapper.selectListAll();
        // 如果 获取的数据 是 空的 说明 当前数据时 第一版本
        if (!listAll.isEmpty()) {
            for (JjMajorCollect ss : listAll) {
                JjHistoricalMajor h = new JjHistoricalMajor();
                h.setVersionCode(code);
                h.setEpcTwo(ss.getEpcTwo());
                h.setSignName(ss.getSignName());
                h.setSignType(ss.getSignType());
                // 商务 1 - 5
                h.setBiesMajorOne(ss.getBiesMajorOne());
                h.setBiesMajorTwo(ss.getBiesMajorTwo());
                h.setBiesMajorThree(ss.getSkillMajorThree());
                h.setBiesMajorFour(ss.getBiesMajorFour());
                h.setBiesMajorFive(ss.getBiesMajorFive());
                // 技术 1 - 7
                h.setSkillMajorOne(ss.getSkillMajorOne());
                h.setSkillMajorTwo(ss.getSkillMajorTwo());
                h.setSkillMajorThree(ss.getSkillMajorThree());
                h.setSkillMajorFour(ss.getSkillMajorFour());
                h.setSkillMajorFive(ss.getSkillMajorFive());
                h.setSkillMajorSix(ss.getSkillMajorSix());
                h.setSkillMajorEight(ss.getSkillMajorEight());
                jjHistoricalMajorMapper.insert(h);
            }
        } else {
            // 将 第一版数据 添加到数据库中
            for (JjMajorCollect ss : list) {
                JjHistoricalMajor h = new JjHistoricalMajor();
                h.setVersionCode(code);
                h.setEpcTwo(ss.getEpcTwo());
                h.setSignName(ss.getSignName());
                h.setSignType(ss.getSignType());
                // 商务 1 - 5
                h.setBiesMajorOne(ss.getBiesMajorOne());
                h.setBiesMajorTwo(ss.getBiesMajorTwo());
                h.setBiesMajorThree(ss.getSkillMajorThree());
                h.setBiesMajorFour(ss.getBiesMajorFour());
                h.setBiesMajorFive(ss.getBiesMajorFive());
                // 技术 1 - 7
                h.setSkillMajorOne(ss.getSkillMajorOne());
                h.setSkillMajorTwo(ss.getSkillMajorTwo());
                h.setSkillMajorThree(ss.getSkillMajorThree());
                h.setSkillMajorFour(ss.getSkillMajorFour());
                h.setSkillMajorFive(ss.getSkillMajorFive());
                h.setSkillMajorSix(ss.getSkillMajorSix());
                h.setSkillMajorEight(ss.getSkillMajorEight());
                jjHistoricalMajorMapper.insert(h);
            }
        }

        // 数据清空
        jjMajorCollectMapper.cleanJjMajorCollect();

        for (JjMajorCollect r : list) {
            jjMajorCollectMapper.insert(r);
        }

        // 添加 回退记录  taskState 任务标识
        RollbackRecord rollback = new RollbackRecord();

        rollback.setFileName(fileName);
        // 任务标识 基建
        rollback.setTaskState(1);
        // 数据 版本号
        rollback.setVersionCode(code);
        rollbackRecordMapper.insert(rollback);

    }


    @Override
    public void deleteByTaskId(Integer taskId) {
        jjMajorCollectMapper.deleteByTaskId(taskId);
    }

    @Override
    public Page<JjMajorCollect> pageJjMajor(JjMajorParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<JjMajorCollect> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<JjMajorCollect> jjMajorList = jjMajorCollectMapper.selectJjMajorByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = jjMajorCollectMapper.pageJjMajorCount(param);
        page.setTotal(aLong);
        page.setRecords(jjMajorList);

        return page;
    }

    @Override
    public void rollbackHistorical() {
        // 目前历史回退需要做数据判断 这是的历史回退是 物资 直接 数据写死 为 1
        RollbackRecord vo = rollbackRecordMapper.selectRollbackOne(1);
        // 判断 是否 已回退
        if (vo.getRollbackFlag() == 1) {
            throw new CommonException(CommonConstants.ERROR, "当前数据已回退上一版本，无法回退");
        }

        // 获取 历史版本中 版本号最大的数据
        List<JjHistoricalMajor> historical = jjHistoricalMajorMapper.selectJjHistoricalByMaxCode();
        if (!historical.isEmpty()) {
            // 数据 不为空
            // 将 这一版半历史数据 赋值到 关系表中
            // 清楚掉当前关系表中的数据
            jjMajorCollectMapper.cleanJjMajorCollect();

            for (JjHistoricalMajor ss : historical) {
                JjMajorCollect h = new JjMajorCollect();
                h.setEpcTwo(ss.getEpcTwo());
                h.setSignName(ss.getSignName());
                h.setSignType(ss.getSignType());
                // 商务 1 - 5
                h.setBiesMajorOne(ss.getBiesMajorOne());
                h.setBiesMajorTwo(ss.getBiesMajorTwo());
                h.setBiesMajorThree(ss.getBiesMajorThree());
                h.setBiesMajorFour(ss.getBiesMajorFour());
                h.setBiesMajorFive(ss.getBiesMajorFive());
                // 技术 1 - 7
                h.setSkillMajorOne(ss.getSkillMajorOne());
                h.setSkillMajorTwo(ss.getSkillMajorTwo());
                h.setSkillMajorThree(ss.getSkillMajorThree());
                h.setSkillMajorFour(ss.getSkillMajorFour());
                h.setSkillMajorFive(ss.getSkillMajorFive());
                h.setSkillMajorSix(ss.getSkillMajorSix());
                h.setSkillMajorEight(ss.getSkillMajorEight());
                jjMajorCollectMapper.insert(h);
            }
        }

        // 回退成功 将其 修改为已回退
        rollbackRecordMapper.updateRollbackFlag(vo.getId());
    }

    @Override
    public List<JjMajorCollect> selectListAll() {
        return jjMajorCollectMapper.selectListAll();
    }

    @Override
    public int updateJjMajorCollect(JjMajorCollect jjMajorCollect) {
        return jjMajorCollectMapper.updateMajor(jjMajorCollect);
    }
}
